package com.planw.beetl.action

import com.intellij.codeInsight.navigation.action.GotoDeclarationUtil
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiIdentifier
import com.intellij.psi.PsiReference
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.refactoring.suggested.endOffset
import com.intellij.refactoring.suggested.startOffset
import com.intellij.util.containers.toArray
import com.planw.beetl.findUsages.BeetlSqlFindUsagesProvider
import com.planw.beetl.utils.JavaService
import org.apache.commons.lang3.StringUtils
import org.intellij.plugins.markdown.lang.psi.impl.MarkdownHeaderImpl

/**
 * BeetlSql Ctrl + 鼠标单击跳转查询
 */
open class BeetlSqlGotoDeclarationHandler : GotoDeclarationHandler {

    override fun getGotoDeclarationTargets(element: PsiElement?, offset: Int, editor: Editor?): Array<PsiElement>? {
        val methods = BeetlSqlFindUsagesProvider.findUsagesMapperMethod(element)
//        val usegeList = mutableListOf<PsiElement>()
//        methods?.forEach {
//            it.nameIdentifier?.let {
//                val els = GotoDeclarationUtil.findTargetElementsFromProviders(it, it.startOffset, editor!!);
//                usegeList.addAll(els)
//            }
//        }
//        if (!usegeList.isEmpty()) {
//            return usegeList?.toTypedArray()
//        }
//        if(methods != null){
//            val list = ReferencesSearch.search(methods[0]).findAll() as List<*>;
//            if(list != null && list.isNotEmpty()){
//               return list?.map {
//                    val el = it as PsiReference
//                    el.element
//                }.toTypedArray()
//            }
//        }
        return methods?.toTypedArray()
    }


}